package http;

import com.xr.util.HttpUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.net.URLDecoder;
import java.util.Collections;
import java.util.function.Consumer;

/**
 * Created by Arnold on 2018-12-11 17:46.
 */
public class HttpDownloader {

  private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:27.0) Gecko/20100101 Firefox/27.0";

  public HttpDownloader() throws Exception {

    download01();

  }

  public void download01() throws Exception {
    final String page_url = "http://192.168.11.19/prd/%E4%BA%A7%E5%93%81/%E6%8A%80%E6%9C%AF/%E6%9E%81%E5%AE%A2%E6%97%B6%E9%97%B4/%e7%8e%a9%e8%bd%acGit%e4%b8%89%e5%89%91%e5%ae%a2/";
    final String save_path = "E:/玩转Git三剑客";
    Document document = Jsoup.connect(page_url).userAgent(USER_AGENT).get();

    Elements elements = document.getElementsByTag("a");
    elements.forEach(new Consumer<Element>() {
      @Override
      public void accept(Element element) {
        try {
          String title = element.html();
          if(!title.startsWith("0")){
            return;
          }

          String url = page_url + element.attr("href");
          download(url, save_path);
//        System.out.println(title+"____"+url);
        } catch (Exception e){
          e.printStackTrace();
        }
      }
    });
  }

  public void download(String url, String savePath) throws Exception {
    String fileName = url.substring(url.lastIndexOf("/"));
    if(fileName == null || fileName.trim().length()==0){
      fileName = String.valueOf(System.currentTimeMillis());
    }
    fileName = fileName.trim();
    fileName = URLDecoder.decode(fileName, "UTF-8");
    byte[] data = HttpUtils.getBytes(url, Collections.emptyMap());
    FileOutputStream fos = new FileOutputStream(savePath+"/"+fileName);
    try{
      fos.write(data);
      fos.flush();
      System.out.println("save file:"+fileName+", to path:"+savePath);
    }finally {
      fos.close();
    }
  }

  public static void main(String[] args) throws Exception {
    new HttpDownloader();
  }

}
